﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.ObjectModel;
using System.Diagnostics;

namespace PasswordProvider.ObjectModel
{
    public class SiteCollection : ObservableCollection<Site>
    {        
        public Site this[Guid id]
        {
            get
            {
                foreach (Site site in this)
                {
                    if (site.Guid == id)
                        return site;
                }
                return null;
            }
        }

        internal SecurityManager SecurityManager { get; set; }

        #region Overrides

        public new void Add(Site site)
        {
            Debug.Assert(site != null);
            
            // Check authentication only if current has been applied (if serializing, it will not have been set yet.
            if (SecurityManager != null)
                if(!SecurityManager.IsAuthenticated)
                    throw new AuthenticationException("Authentication is required to add an item.");

            base.Add(site);
        }

        public void Remove(Guid siteId)
        {
            Remove(this[siteId]);
        }

        public new void Remove(Site site)
        {
            Debug.Assert(SecurityManager != null);
            Debug.Assert(site != null);

            if (!SecurityManager.IsAuthenticated)
                throw new AuthenticationException("Authentication is required to remove an item.");

            base.Remove(site);
            
        }

        public new void RemoveAt(int index)
        {
            if (!SecurityManager.IsAuthenticated)
                throw new AuthenticationException("Authentication is required to remove an item.");

            base.RemoveAt(index);
        }

        public new void Insert(int index, Site item)
        {
            if (!SecurityManager.IsAuthenticated)
                throw new AuthenticationException("Authentication is required to insert an item.");

            base.Insert(index, item);
        }

        public override string ToString()
        {
            StringBuilder builder = new StringBuilder();

            foreach (Site site in this)
            {
                builder.Append(site.WriteExportString(SecurityManager,true));
                builder.Append("\n");
            }

            return builder.ToString();
        }

        /// <summary>
        /// Removes the tag from every site that contains it.
        /// </summary>
        public void DeleteTag(string tag)
        {
            if (!SecurityManager.IsAuthenticated)
                throw new AuthenticationException("Authentication is required to delete tags.");
            
            foreach (Site site in this)
            {
                while(site.Tags.Remove(tag))
                    ; // do nothing.
            }

            FileHelper.Save();

        }


        #endregion // Overrides
        
    }
}
